package com.sun.showcase.controller.app.mall;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.sun.showcase.biz.mall.service.AddressService;
import com.sun.showcase.client.query.mall.AddressQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
@CrossOrigin
@Controller("appAddressController")
@RequestMapping(value="/app/address") 
public class AddressController{
	
	@Autowired
	private AddressService addressService;
	
	private Result result = new Result();

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public Result datagrid(AddressQuery addressQuery) {
		if(StringUtils.isEmpty(addressQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效用户！");
			return result;
		}
		DataGrid dg = addressService.datagrid(addressQuery);
		result.setData(dg);
		result.setSuccess(true);
		return result;
	}
	

	/**
	 * 添加一个Address
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(AddressQuery addressQuery) {
		if(StringUtils.isEmpty(addressQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效用户！");
			return result;
		}
		addressService.add(addressQuery);
		result.setSuccess(true);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑Address
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(AddressQuery addressQuery) {
		addressService.update(addressQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}
	/**
	 * 删除Address
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(AddressQuery addressQuery) {
		if(StringUtils.isEmpty(addressQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效用户！");
			return result;
		}
		addressService.delete(addressQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	/**
	 * 全部数据Address
	 */
	@ResponseBody
	@RequestMapping(value="listAll")
	public Result listAll(AddressQuery addressQuery) {
		if(StringUtils.isEmpty(addressQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效用户！");
			return result;
		}
		List<AddressQuery> list = addressService.listAll(addressQuery);
		result.setSuccess(true);
		result.setMsg("成功！");
		result.setData(list);
		return result;
	}
	
	/**
	 * 全部数据Address
	 */
	@ResponseBody
	@RequestMapping(value="default")
	public Result getDefault(String userId) {
		if(StringUtils.isEmpty(userId)) {
			result.setSuccess(false);
			result.setMsg("无效用户！");
			return result;
		}
		AddressQuery addressQuery = new AddressQuery();
		addressQuery.setUserId(userId);
		addressQuery.setIfDefaultAddress("1");
		addressQuery.setActiveFlag("1");
		List<AddressQuery> list = addressService.listAll(addressQuery);
		result.setSuccess(list.size()>=1);
		if(result.isSuccess()) {
			result.setData(list.get(0));
		}
		return result;
	}
}
